package com.yc.fav.util;

import com.google.gson.Gson;
import com.yc.fav.service.BizException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;

public class Utils {
    public static void check(boolean bool,String msg,int code) throws BizException {
    if(bool){
        throw new BizException(msg,code);
    }
    }
    public static void checkEmpty(Object obj,String msg,int code) throws BizException {
        if(obj==null){
            throw new BizException(msg,code);
        }else if(obj instanceof String){
            String s=(String)obj;
            if(s.isBlank()){
                throw new BizException(msg,code);
            }
        }else if(obj instanceof Collection){
            Collection c=(Collection)obj;
            if(c.isEmpty()){
                throw new BizException(msg,code);
            }
        }
    }
    public static void output(HttpServletResponse response, Object data) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        String json=new Gson().toJson(data);
        response.getWriter().write(json);
    }
}
